<template>
  <view>
    <view class="goods-container">
      <view v-for="item in goodsList" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
        <my-goods :item="item"></my-goods>      
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        onloading: false
      };
    },
    
    onLoad(option) {
      this.goodsObj.query = option.query || '',
      this.goodsObj.cid = option.cid || '',
      this.getGoodsList()
    },
    
    
    methods: {
      // 获取商品列表
      async getGoodsList(cd) {
        this.onloading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.goodsObj)
        cd && cd()
        if(res.meta.status !== 200) return uni.showMsg('商品列表数据获取失败')
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
        this.onloading = false
      },
      
      // 跳转到商品详情页
      gotoDetail(id) {
        uni.navigateTo({
          url: '/subpkg/goods-detail/goods-detail?goods_id=' + id
        })
      }
      
    },
    
    // 上拉加载更多
      onReachBottom() {
        if(this.goodsObj.pagenum * this.goodsObj.pagesize >= this.total) return uni.showMsg('数据加载完毕！')
        if(this.onloading) return
        this.goodsObj.pagenum ++
        this.getGoodsList()
      },
    
    // 下拉刷新页面
    onPullDownRefresh() {
      this.goodsObj.pagenum = 1
      this.total = 0,
      this.goodsList = [],
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">
</style>
